﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// guanzhu 的摘要说明
    /// </summary>
    public class guanzhu : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            //取得处事类型
            string action = DTRequest.GetQueryString("action");

            switch (action)
            {
                case "set_guanzhu": //设置关注
                    set_guanzhu(context);
                    break;
                case "guanzhu_list": //删除供应信息
                    guanzhu_list(context);
                    break;
                case "call_list": //删除供应信息
                    call_list(context);
                    break;
            }
        }

        #region 我发布的物流信息=============================
        private void set_guanzhu(HttpContext context)
        {
            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            int _gz_userid = DTRequest.GetFormInt("gz_userid");

            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"尚未登录，不能关注！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"尚未登录，不能关注！\"}");
                return;
            }

            BLL.guanzhu bll = new BLL.guanzhu();
            //先判断是否有收藏过
            if (bll.Exists(_gz_userid, userModel.id))
            {
                bool result = bll.Delete(_gz_userid, userModel.id);
                if (result)
                {
                    context.Response.Write("{\"status\": 1, \"is_cancel\": 1, \"msg\": \"取消成功！\"}");
                }
                return;
            }
            Model.guanzhu model = new Model.guanzhu();

            model.gz_userid = _gz_userid;
            model.user_id = userModel.id;
            model.add_time = DateTime.Now;

            if (bll.Add(model) > 0)
            {
                context.Response.Write("{\"status\": 1, \"is_cancel\": 0, \"msg\": \"关注成功！\"}");
                return;
            }
            context.Response.Write("{\"status\": 0, \"msg\": \"系统发生错误！\"}");
            return;
        }
        #endregion

        #region 关注列表=============================
        private void guanzhu_list(HttpContext context)
        {
            string _user_name = DTRequest.GetString("txtUserName");
            string _user_pwd = DTRequest.GetString("txtPassword");

            int page_index = DTRequest.GetQueryInt("page");
            int page_size = 15;
            int totalcount;

            if (string.IsNullOrEmpty(_user_name) || string.IsNullOrEmpty(_user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"错误提示：用户名或密码错误！\"}");
                return;
            }

            Model.users userModel = new BLL.users().GetModel(_user_name, _user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"错误提示：用户名或密码错误！\"}");
                return;
            }

            StringBuilder strTxt = new StringBuilder();
            if (page_size == 0)
            {
                context.Response.Write("获取失败，传输参数有误！");
                return;
            }

            BLL.guanzhu bll = new BLL.guanzhu();
            DataSet ds = bll.GetList(page_size, page_index, "user_id=" + userModel.id, "id desc", out totalcount);
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"gz_userid\":" + dr["gz_userid"]);

                    string _gz_username = new BLL.users().GetUserName(Convert.ToInt32(dr["gz_userid"]));
                    strTxt.Append(",\"gz_username\":\"" + _gz_username + "\"");
                    string _avatar = new BLL.users().GetAvatar(Convert.ToInt32(dr["gz_userid"]));
                    if (string.IsNullOrEmpty(_avatar))
                    {
                        strTxt.Append(",\"gz_avatar\":\"../image/default_avatar.png\"");
                    }
                    else
                    {
                        if (_avatar.Contains("shicaiquanzi"))
                        {
                            strTxt.Append(",\"gz_avatar\":\"http://" + _avatar + "\"");
                        }
                        else
                        {
                            strTxt.Append(",\"gz_avatar\":\"http://www.shicaiquanzi.com" + _avatar + "\"");
                        }
                    }
                    string _gz_mobile = new BLL.users().GetMobile(Convert.ToInt32(dr["gz_userid"]));
                    strTxt.Append(",\"gz_mobile\":\"" + _gz_mobile + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                context.Response.Write("{\"status\":2}");
                return;
            }
            context.Response.Write(strTxt.ToString());
        }
        #endregion

        #region 通话列表=============================
        private void call_list(HttpContext context)
        {
            string _user_name = DTRequest.GetString("txtUserName");
            string _user_pwd = DTRequest.GetString("txtPassword");

            int page_index = DTRequest.GetQueryInt("page");
            int page_size = 15;
            int totalcount;

            if (string.IsNullOrEmpty(_user_name) || string.IsNullOrEmpty(_user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"错误提示：用户名或密码错误！\"}");
                return;
            }

            Model.users userModel = new BLL.users().GetModel(_user_name, _user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"错误提示：用户名或密码错误！\"}");
                return;
            }

            StringBuilder strTxt = new StringBuilder();
            if (page_size == 0)
            {
                context.Response.Write("获取失败，传输参数有误！");
                return;
            }

            BLL.call_log bll = new BLL.call_log();
            DataSet ds = bll.GetList(page_size, page_index, "send_user_id=" + userModel.id, "id desc", out totalcount);
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"to_user_id\":" + dr["to_user_id"]);
                    strTxt.Append(",\"to_username\":\"" + dr["to_user_name"] + "\"");
                    string _avatar = new BLL.users().GetAvatar(Convert.ToInt32(dr["to_user_id"]));
                    if (string.IsNullOrEmpty(_avatar))
                    {
                        strTxt.Append(",\"avatar\":\"../image/default_avatar.png\"");
                    }
                    else
                    {
                        if (_avatar.Contains("shicaiquanzi"))
                        {
                            strTxt.Append(",\"avatar\":\"http://" + _avatar + "\"");
                        }
                        else
                        {
                            strTxt.Append(",\"avatar\":\"http://www.shicaiquanzi.com" + _avatar + "\"");
                        }
                        
                    }
                    strTxt.Append(",\"mobile\":\"" + dr["mobile"] + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("]");
            }
            else
            {
                context.Response.Write("{\"status\":2}");
                return;
            }
            context.Response.Write(strTxt.ToString());
        }
        #endregion
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}